﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class BgmSourceDAL : IDALBgmSource
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public BgmSourceDAL()
        {

        }

        public IList<BgmSourceInfo> GetBgmSource()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<BgmSourceInfo> infoList = new List<BgmSourceInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT s.BGM_SOURCE_SEQ"
                            + ", s.BGM_SOURCE_NAME"
                            + ", t.BGM_SOURCE_TYPE_SEQ"
                            + ", t.BGM_SOURCE_TYPE_NAME"
                        + " FROM BGM_MAS_SOURCE s"
                            + " INNER JOIN BGM_MAS_SOURCE_TYPE t ON s.BGM_SOURCE_TYPE_SEQ = t.BGM_SOURCE_TYPE_SEQ"
                        + " WHERE s.RECORD_STATUS <> @delete_status"
                            + " AND s.BGM_SOURCE_SEQ NOT IN "
                                + " (select upper_bgm_source_seq from bgm_mas_source where upper_bgm_source_seq is not null and record_status <> @delete_status)";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        BgmSourceInfo info = new BgmSourceInfo();
                        BgmSourceTypeInfo infoSourceType = new BgmSourceTypeInfo();
                        info.Seq = Convert.ToInt32(dr[i]["BGM_SOURCE_SEQ"]);
                        info.Name = (dr[i]["BGM_SOURCE_NAME"]).ToString();
                        infoSourceType.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_TYPE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_TYPE_SEQ"]));
                        infoSourceType.Name = (dr[i]["BGM_SOURCE_TYPE_NAME"]).ToString();
                        info.InfoSourceType = infoSourceType;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
